<#include "include/head.ftl">
package ${NamespaceController};

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
 import com.ferryc.commons.utils.StringUtil;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ResultConstant;
import ${NamespaceModel}.${Po};
import ${NamespaceModel}.param.${Po}Param;
import ${NamespaceService}.I${Po}Service;

 
/**
 * 
 * 类名:			${Po}Controller  <br/>
 * 作用：			${tableLabel}控制<br/>
 * 方法： 				<br/>
 * 创建者:			${author} <br/>
 * 添加时间: 		${copyright.createDate} ${copyright.createTime} <br/>
 
 */
@Controller
@RequestMapping("/${viewDir}")
public class ${Po}Controller extends BaseController  {
 
   /** */
 	@Autowired
	private I${Po}Service ${Po?uncap_first}Service;
	 
	 
	/** */
	@GetMapping("/")
	public String manage(Model model) {
		return "sys/${viewDir}/list";
	}

	/**
	 * 列表查询
	 * 
	 * @param ${Po?uncap_first}Param
	 *            请求参数
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public Object list(${Po}Param ${Po?uncap_first}Param) {
		${Po?uncap_first}Param.setStatus(StringUtil.EMPTY);
		return ${Po?uncap_first}Service.selectPage(${Po?uncap_first}Param.to${Po}());
	}
 
	/**
	 * 进入新增页面
	 * 
	 * @param sysLogInfoParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 *            request
	 * @return
	 */
	@GetMapping("/addPage")
	public Object addPage(${Po}Param ${Po?uncap_first}Param, Model model, HttpServletRequest request) {
		model.addAttribute(GlobalConstant.PARENT_ID, request.getParameter(GlobalConstant.PARENT_ID));
		return "${pageName}/${viewDir}/add";
	}

	/**
	 * 新增保存
	 * 
	 * @param ${Po?uncap_first}Param
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@PostMapping("/insert")
	@ResponseBody
	public Object insert(@Valid ${Po}Param ${Po?uncap_first}Param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		 
		${Po?uncap_first}Param.preInsert();
		${Po?uncap_first}Service.insert(${Po?uncap_first}Param.to${Po}());
		return renderSuccess(ResultConstant.INSERT_SUCCESS);
	}

	/**
	 * 进入查看页面
	 * 
	 * @param ${Po?uncap_first}Param
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@GetMapping("/viewPage")
	public Object viewPage(${Po}Param ${Po?uncap_first}Param, Model model, HttpServletRequest request) {
		${Po?uncap_first}Param.setStatus(StringUtil.EMPTY);
		${Po} ${Po?uncap_first} = ${Po?uncap_first}Service.get(${Po?uncap_first}Param.getId());
		model.addAttribute("info", ${Po?uncap_first});
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.TRUE);
		return "${pageName}/${viewDir}/edit";
	}

	/**
	 * 进入修改页面
	 * 
	 * @param ${Po?uncap_first}Param
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@GetMapping("/editPage")
	public Object editPage(${Po}Param ${Po?uncap_first}Param, Model model, HttpServletRequest request) {
		${Po?uncap_first}Param.setStatus(StringUtil.EMPTY);
		${Po} ${Po?uncap_first} = ${Po?uncap_first}Service.get(${Po?uncap_first}Param.getId());
		model.addAttribute("info", ${Po?uncap_first});
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.FALSE);
		return "${pageName}/${viewDir}/edit";
	}

	/**
	 * 修改
	 * 
	 * @param ${Po?uncap_first}Param
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@PostMapping("/save")
	@ResponseBody
	public Object save(@Valid ${Po}Param ${Po?uncap_first}Param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		${Po?uncap_first}Param.preUpdate();
		${Po?uncap_first}Service.update(${Po?uncap_first}Param.to${Po}());
		return renderSuccess(ResultConstant.UPDATE_SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param ${Po?uncap_first}Param
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(${Po}Param ${Po?uncap_first}Param, BindingResult bindingResult) {
		 ${Po?uncap_first}Param.preUpdate();
		 ${Po?uncap_first}Param.setDelFlag(GlobalConstant.YES);
		${Po?uncap_first}Service.update( ${Po?uncap_first}Param.to${Po}());
		return renderSuccess(ResultConstant.DELETE_SUCCESS);
	}
 
	
}